﻿using System.Collections;
using System.Collections.Generic;

namespace SLS.ExClassLib.MultiThreading.Patterns.Chains
{
    public abstract class ChainBase<T> : IChain<T>
    {
        #region Implementation of IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            IChain<T> current = this;
            while (current != null)
            {
                yield return current.Value;
                current = current.Next;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region Implementation of IChain<T>

        public abstract IChain<T> Next { get; }

        public bool HasNext
        {
            get { return Next != null; }
        }

        public abstract T Value { get; }

        public int Count
        {
            get
            {
                int count = 0;
                IChain<T> current = this;
                while (current != null)
                {
                    count++;
                    current = current.Next;
                }
                return count;
            }
        }

        #endregion
    }
}